
//外部数据源弹窗
function extdataselect(id) {
    var iHeight=400;
    var iWidth=700;
    var iTop = (window.screen.height-30-iHeight)/2; //获得窗口的垂直位置;
    var iLeft = (window.screen.width-10-iWidth)/2; //获得窗口的水平位置;
    window.open('/TerpServerController/dataSelectPreview?id='+id,"",'height='+iHeight+',,innerHeight='+iHeight+',width='+iWidth+',innerWidth='+iWidth+',top='+iTop+',left='+iLeft+',toolbar=no,menubar=no,scrollbars=auto,resizeable=no,location=no,status=no');
}

//内部数据源弹窗
function dataselect(id) {
    var iHeight=400;
    var iWidth=700;
    var iTop = (window.screen.height-30-iHeight)/2; //获得窗口的垂直位置;
    var iLeft = (window.screen.width-10-iWidth)/2; //获得窗口的水平位置;
    window.open('/TerpServerController/dataSelect?id='+id,"",'height='+iHeight+',,innerHeight='+iHeight+',width='+iWidth+',innerWidth='+iWidth+',top='+iTop+',left='+iLeft+',toolbar=no,menubar=no,scrollbars=auto,resizeable=no,location=no,status=no');
}

//因为兼容问题移动端改为弹出层
function extdataselectApp(type,id){
    if (type==='1' || type===1){
        type='extdataselect';
    }else if(type==='2' || type===2){
        type='dataselect';
    }
    layer.open({
        type: 2,
        area: ['90%', '90%'],
        maxmin:true,
        content:'/TerpServerController/dataSelectPreviewApp?type='+type+'&id='+id
        
    });
}

function closeAll() {
    layer.closeAll();
}

//获取全部按钮，删除在附加做出替换
function outData(data) {
    $('button[data-type="extdataselect"]').each(function(index,element){
        var name=$(element).attr('name');
        var id=$(element).attr('id');
        var title=$(element).attr('title');
        var data_table_name=$(element).attr('data_table_name');
        var data_db_id=$(element).attr('data_db_id');
        var data_field=$(element).attr('data_field');
        var data_fld_name=$(element).attr('data_fld_name');
        var data_control=$(element).attr('data_control');
        var data_querys=$(element).attr('data_querys');
        var terpid=$(element).attr('terpid');
        var str=id.split("_");
        var imgs='<i name="'+name+'" ' +
            'id="'+id+'" ' +
            'class="form_item dataselect layui-icon layui-icon-face-smile layui-icon layui-icon-add-1" ' +
            'data-type="extdataselect" ' +
            'title="'+title+'" ' +
            'readonly="readonly" ' +
            'style="height:auto; ' +
            'width:auto; font-size: 30px; color: #1E9FFF;" ' +
            'onclick="extdataselect('+str[str.length-1]+')" ' +
            'data_table_name="'+data_table_name+'"' +
            'data_db_id="'+data_db_id+'" ' +
            'data_field="'+data_field+'" ' +
            'data_fld_name="'+data_fld_name+'" ' +
            'data_control="'+data_control+'" ' +
            'terpid="'+terpid+'"' +
            'data_querys="'+data_querys+'"></i>';
        var paren=$(element).parent();
        $(element).remove();
        $(paren).append(imgs);
    });
}


function internalData(data) {
    $('button[data-type="dataselect"]').each(function (index, element) {
        var way=$(element).attr('way');
        if (way!='0'){
            $(element).css('display','none'); //隐藏控件
            var data_table=$(element).attr('data_table'); //表名
            var data_field=$(element).attr('data_field').split(','); //字段
            var data_control=$(element).attr('data_control').split(','); //关联的控件名
            var data_querys=$(element).attr('data_querys').split(','); //是否是查询字段
            $.ajax({
                url: "/TerpServerController/selectapptable",
                type: 'get',
                data: {dName:"data_"+data_table},
                dataType: 'json',
                success: function (res) {
                    var obj=res.obj;
                    //添加select属性
                    for(var j=0;j<data_field.length;j++){
                        //判断是否是查询字段如果是才可以进行添加select属性
                        if (data_querys[j]==='1'){
                            var option=' <datalist id="'+data_field[j]+'" >\n';
                            for (var i=0;i<obj.length;i++){
                                option+='<option style="display: block">'+obj[i][data_field[j]]+'</option>';
                            }
                            option+='</datalist>';

                            $('input[title="'+data_control[j]+'"]').append(option); //追加下拉框
                            $('input[title="'+data_control[j]+'"]').attr('list',data_field[j]); //添加select属性
                        }
                    }

                },
                error: function () {
                }
            });

        }else {
            var id=$(element).attr('id');
            var str=id.split("_");
            $(element).attr('onclick','dataselect('+str[str.length-1]+')');
        }

    });
}

